// index.js
import api from '@/utils/request'
import {formatterTime} from '@/utils/data'
Page({
  data: {
    bannerList: [],
    topIcon: [{
        icon: 'http://121.37.178.3:48090/admin-api/infra/file/23/get/lead.webp',
        text: '党建引领',
        color: 'linear-gradient(180deg, #08AEEA 0%, #2a5af5 100%)',
        key: 'lead'
      },
      {
        icon: 'http://121.37.178.3:48090/admin-api/infra/file/23/get/group.webp',
        text: '大统战+大群团',
        color: 'linear-gradient(180deg, #FAD961 0%, #F76B1C 100%)',
        key: 'big'
      }, {
        icon: 'http://121.37.178.3:48090/admin-api/infra/file/23/get/guide.png',
        text: '办事指南',
        color: 'linear-gradient(180deg, #fac361 0%, #b3521b 100%)',
        key: 'libraryflow'
      }
    ],
    iconList:[{
      icon:'/image/images/icon_org.png',
      text:'统战职能',
      key:'org'
    },{
      icon:'/image/images/icon_start.png',
      text:'雨湖之星',
      key:'star'
    },{
      icon:'/image/images/icon_heart.png',
      text:'同心互联',
      key:'appid'
    },{
      icon:'/image/images/icon_proposal.png',
      text:'同心建言',
      key:'suggest'
    },{
      icon:'/image/images/icon_zk.png',
      text:'智库资源',
      key:'resources'
    },
    // ,{
    //   icon:'/image/images/icon_repository.png',
    //   text:'知识库',
    //   key:'library'
    // }
    // {
    //   icon:'/image/images/sg.png',
    //   text:'时光掠影',
    //   key:'video'
    // },
    // {
    //   icon:'/image/images/icon_feast_day.png',
    //   text:'宗教节日',
    //   key:'date'
    // }
    // ,{
    //   icon:'',
    //   text:'',
    //   key:''
    // }
  ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 4000,
    duration: 500,
    policyParams: {
      pageNo: 1,
      pageSize: 6,
      newsType: 'info'
    },
    infoList: [],
    infoTotal: 0,
    policyList: [],
    policyTotal: 0,
    active: 0,
    activeList: [],
    activeParams: {
      pageNo: 1,
      pageSize: 6,
      activityStatus: 'in_progress'
    },
    studyParams: {
      pageNo: 1,
      pageSize: 6,
    },
    studyList: [],
    suggestList: [],
    suggestParams: {
      pageNo: 1,
      pageSize: 10,
    },
    knowledgeTypeList: [{
      label: '知识库',
      key: 'library',
      icon: 'http://121.37.178.3:48090/admin-api/infra/file/23/get/6383bcc8ecce7800c580cf385c949fddd76cbd3e6fea9-2Vcf22_fw240webp.webp'
    }, {
      label: '同心政策',
      key: 'policy',
      icon: 'http://121.37.178.3:48090/admin-api/infra/file/23/get/1519041805ec5bfe3fed933052b0dc5773442d3c724e7-PT9BIC_fw240webp.webp'
    }],
    // serviceList: [{
    //   name: '时光掠影',
    //   image: '/image/images/sg.png'
    // }, {
    //   name: '智库资源',
    //   image: '/image/images/zk.png'
    // }, {
    //   name: '同心建言',
    //   image: '/image/images/jy.png'
    // }, {
    //   name: '同心互联',
    //   image: '/image/images/hl.png'
    // }, {
    //   name: '雨湖之星',
    //   image: '/image/images/dr.png'
    // }],
    policyCurrent: 0,
    InfoCurrent: 0,

    activityType: 'in_progress',
    activeStatus: [],
    scrollX: 0
  },
  getActiveStatus() {
    api.get('/dict/getByType?type=work_tasks_status').then(res => {
      this.setData({
        activeStatus: res
      })
    })
  },
  bindIcon({
    currentTarget
  }) {
    console.log(currentTarget.dataset)
    if (currentTarget.dataset.key == 'star') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'org') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'study') {

    } else if (currentTarget.dataset.key == 'resources') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'video') {
      wx.navigateTo({
        url: '/subPackages/pages/video/video'
      })
    } else if (currentTarget.dataset.key == 'suggest') {
      wx.navigateTo({
        url: '/subPackages/pages/suggest/suggest',
      })
    } else if (currentTarget.dataset.key == 'library') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'libraryflow') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'date') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'lead') {
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    } else if (currentTarget.dataset.key == 'big') {
      wx.navigateTo({
        url: '/subPackages/pages/h5/h5?skip=https://m.voc.com.cn/portal/topic/show/tid/206047'
      })
    } else if (currentTarget.dataset.key == 'appid') {
      // this.jump()
      wx.navigateTo({
        url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.key,
      })
    }
  },
  scrollService(e) {
    this.setData({
      scrollX: e.detail.deltaX
    })
    console.log(e.detail.deltaX)
  },
  // onChange(event) {
  //   this.setData({
  //     active: event.detail.name,
  //     infoList: [],
  //     policyList: []
  //   })
  //   if (this.data.active == 0) {
  //     this.setData({
  //       'policyParams.newsType': 'info'
  //     })
  //     this.getPolicyList()
  //   } else if (this.data.active == 1) {
  //     this.setData({
  //       'policyParams.newsType': 'policy'
  //     })
  //     this.getPolicyList()
  //   } else if (this.data.active == 3) {
  //     this.getActiveList()
  //   }
  // },

  async onLoad(options) {
    this.getBannerList()

  },
  onShow() {
    // this.getPolicyList('policy')
    this.getActiveStatus()
    this.getInfoList('info')
    this.getStudyList()
    this.getActiveList()
    this.getSuggestList()
    // this.getPolicyTypeList()
    if (typeof this.getTabBar == 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        // 这里的数字对应tabbar对应的索引
        selected: 0,
      });
    }
  },
  jumpToH5: function ({
    currentTarget
  }) {
    // console.log(e)
    wx.navigateTo({
      url: '/subPackages/pages/h5/h5?skip=' + currentTarget.dataset.skip
    })
  },
  toSuggestForm({
    currentTarget
  }) {
    console.log(currentTarget.dataset.id, 'currentTarget.dataset.id')
    wx.navigateTo({
      url: '/subPackages/pages/suggestForm/suggestForm?id=' + currentTarget.dataset.id,
    })
  },
  swiperPolicyChange: function (e) {
    this.setData({
      policyCurrent: e.detail.current //获取当前轮播图片的下标
    })
  },
  swiperInfoChange: function (e) {
    this.setData({
      InfoCurrent: e.detail.current //获取当前轮播图片的下标
    })
  },
  getPolicyList(data) {
    console.log(data)
    this.setData({
      'policyParams.newsType': data
    })
    api.get('/news/page', this.data.policyParams).then(res => {
      this.setData({
        // [...this.data.policyList, ...res.list]
        policyList: res.list,
        policyTotal: res.total
      })
    })
  },
  getInfoList(data) {
    console.log(data)
    this.setData({
      'policyParams.newsType': data
    })
    console.log(this.data.policyParams)
    api.get('/news/page', this.data.policyParams).then(res => {
      this.setData({
        infoList: res.list,
        // infoList: [...this.data.infoList, ...res.list],
        infoTotal: res.total
      })
      // if (this.data.policyParams.newsType == 'info') {

      // } else if (this.data.policyParams.newsType == 'policy') {
      //   this.setData({
      //     // [...this.data.policyList, ...res.list]
      //     policyList: res.list,
      //     policyTotal: res.total
      //   })
      //   console.log('----')
      // }
    })
  },
  getBannerList() {
    api.get('/graphics/banner').then(res => {
      this.setData({
        bannerList: res
      })
    })
  },
  toVideo() {
    wx.navigateTo({
      url: '/subPackages/pages/video/video'
    })
  },
  toResources({
    currentTarget
  }) {
    wx.navigateTo({
      url: '/subPackages/pages/list/list?type=resources'
    })
  },
  bindActvie({
    currentTarget
  }) {
    this.setData({
      activityType: currentTarget.dataset.type,
      'activeParams.activityStatus': currentTarget.dataset.type
    })
    this.getActiveList()
  },
  getActiveList() {
    api.get('/activity/page', this.data.activeParams).then(res => {
      this.setData({
        activeList: res.list.map(val => {
          // console.log(formatterTime(startDate,5,14))
          return {
            ...val,
            startDate: formatterTime(val.startDate,5,16),
            endDate: formatterTime(val.endDate,5,16)
          }
          // console.log(val.startDate.slice(0,4)==year?val.startDate.substring(5):val.startDate)
        })
      })
    })
  },
  getStudyList() {
    api.get('/learning/page', this.data.studyParams).then(res => {
      this.setData({
        studyList: res.list
      })
    })
  },
  getSuggestList() {
    api.get('/suggest/getSubjectList', this.data.suggestParams).then(res => {
      this.setData({
        suggestList: res.list
      })
    })
  },
  toDetail({
    currentTarget
  }) {
    wx.navigateTo({
      url: '/subPackages/pages/detail/detail?id=' + currentTarget.dataset.id + '&type=' + currentTarget.dataset.type,
    })
  },

  toList({
    currentTarget
  }) {
    wx.navigateTo({
      url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.type,
    })
  },
  toPolicyList({
    currentTarget
  }) {
    wx.navigateTo({
      url: '/subPackages/pages/list/list?type=' + currentTarget.dataset.type + '&cateId=' + currentTarget.dataset.cateid,
    })
  },
  toSuggest() {
    wx.navigateTo({
      url: '/subPackages/pages/suggest/suggest',
    })
  },
  onReachBottom() {
    // policyParams:{
    //   pageNo:1,
    //   pageSize:10,
    //   newsType:'info'
    // },
    if (this.data.policyParams.newsType == 'info') {
      if (this.data.infoTotal > this.data.infoList.length) {
        this.setData({
          'policyParams.pageNo': this.data.policyParams.pageNo + 1
        })
        this.getPolicyList()
      }
    } else if (this.data.policyParams.newsType == 'policy') {
      if (this.data.policyTotal > this.data.policyTotal.length) {
        // this.setData({policyList:[]})
        this.setData({
          'policyParams.pageNo': this.data.policyParams.pageNo + 1
        })
        this.getPolicyList()
      }
    }

  },
})